import { getLodop } from '@/utils/LodopFuncs';
import { DecimalUtils } from '@/utils/math'
import { parseTime4 } from '@/utils';
import { Message } from 'element-ui';
import store from '@/store'
var LODOP, P_ID = '', TaskID1, TaskID2, t, c = 0; //声明为全局变量
// 检查是否安装打印控件
export function checkPrinter() {
	let LODOP = getLodop();
	if (!LODOP) return false;
	return true;
}
// 批量打印拣货单生成
export function print(data) {
	if (Array.isArray(data)) {
		for (let i = 0; i < data.length; i++) {
			printSignal(data[i]);
		}
	} else {
		printSignal(data);
	}
}
// 打印拣货单
function printSignal(data) {
	const printPageSet = store.getters.printSet; // 0: A4  1: 三联打印
	let width = ''  // 打印纸张宽度
	let textWidth = ''  // 打印内容区域大小
	let tableHeight = 310;  // 打印表格高度
	LODOP = getLodop();
	let left = 0  // 打印初始距离左侧位置
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	if (printPageSet === '0') {
		textWidth = '19cm'
		tableHeight = 870;
		left = 27;
		LODOP.PRINT_INIT("数航供应链_ERP系统_拣货单打印_A4纸");
		LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4"); // 设置纸张大小
	} else {
		width = '24.1cm'
		textWidth = '21cm';
		tableHeight = 310;
		LODOP.PRINT_INITA(0, 0, width, "139.5mm", "数航供应链_ERP系统_拣货单_拣货单打印_三联纸");
		LODOP.SET_PRINT_PAGESIZE(1, width, "139.5mm", "CreateCustomPage"); // 设置纸张大小
	}
	let { chooseNumber, commodityNum, regionName, groupName } = data;
	LODOP.SET_SHOW_MODE("LANDSCAPE_DEFROTATED", 1);
	LODOP.ADD_PRINT_HTM(20, left, textWidth, 30, `<h1 style="text-align:center;margin:0 auto;font-size:26px">数航供应链统一拣货单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_TABLE(150, left, textWidth, tableHeight, getTableStr(data));
	LODOP.ADD_PRINT_HTM(70, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;">区域:${regionName}</p><p style="margin:0;">品规数:${commodityNum}</p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(110, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;">单号:${chooseNumber}</p><p style="margin:0;">通道组:${groupName}</p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(tableHeight + 150 + 30, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: flex-end;font-weight:bold;font-size:16px;"><p style="margin:0;"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.PRINT_DESIGN();
	LODOP.PRINT();
}
// 拣货单表格生成
export function getTableStr(data) {
	let { chooseDetailsVoList } = data;
	// 假数据
	// chooseDetailsVoList = [...chooseDetailsVoList,...chooseDetailsVoList,...chooseDetailsVoList,...chooseDetailsVoList,...chooseDetailsVoList]
	// chooseDetailsVoList = [...chooseDetailsVoList,...chooseDetailsVoList,...chooseDetailsVoList,...chooseDetailsVoList]
	let style = `<style>
	.print-tab {border-collapse: collapse;width:100% !important;text-align: center;margin: 0 auto;}
	.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
	.print-tab td{font-size:14px}
	</style><body>`;
	let tableStr =
		"<table  class='print-tab'><thead><tr><th style='width:50px;'>序号</th><th>货位号</th><th style='text-align:left'>商品名称</th><th>规格</th><th style='width:40px;'>单位</th><th style='width:40px;'>数量</th><th style='width:68px;'>配送规格</th><th style='width:132px;'>件数（配送规格）</th></tr></thead><tbody>" +
		chooseDetailsVoList
			.map((row, index) => {
				return `<tr>
            <td>${index + 1}</td>
            <td style="font-weight:bold">${row.positionName}</td>
            <td style="text-align:left">${row.commodityName}</td>
            <td>${row.standard}</td>
            <td>${row.unitName}</td>
            <td style="font-weight:bold">${row.procureCount}</td>
            <td style="font-weight:bold">${row.deliveryNorms}</td>
            <td style="font-weight:bold;">${row.procureNumber}</td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}

// 打印随货单
export function printFollow(data, isPDF = false) {
	const printPageSet = store.getters.printSet; // 0: A4  1: 三联打印
	let width = ''  // 打印纸张宽度
	let textWidth = ''  // 打印内容区域大小
	let tableHeight = 280;  // 打印表格高度
	LODOP = getLodop();
	let left = 0  // 打印初始距离左侧位置
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	if (printPageSet === '0') {
		textWidth = '19cm'
		tableHeight = 850;
		left = 27;
		if (isPDF) {
			left = 38;
			LODOP.PRINT_INIT("数航供应链_ERP系统_随货单导出_A4纸");
			if(LODOP.SET_PRINTER_INDEX('Microsoft Print to PDF')){
				Message.success("正在导出...");
			}else {
				Message.error("暂无Microsoft Print to PDF虚拟打印机");
			}
		} else {
			LODOP.PRINT_INIT("数航供应链_ERP系统_随货单打印_A4纸");
		}
		LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4"); // 设置纸张大小
	} else {
		if (isPDF) {
			left = 60;
			LODOP.PRINT_INITA(0, 0, width, "139.5mm", "数航供应链_ERP系统_随货单导出_三联纸");
			if(LODOP.SET_PRINTER_INDEX('导出为WPS PDF')){
				Message.success("正在导出...");
			}else {
				Message.error("暂无导出WPS PDF虚拟打印机");
			}
		} else {
			LODOP.PRINT_INITA(0, 0, width, "139.5mm", "数航供应链_ERP系统_随货单打印_三联纸");
		}
		tableHeight = 280;
		width = '24.1cm'
		textWidth = '21cm'
		LODOP.SET_PRINT_PAGESIZE(1, width, "139.5mm", "CreateCustomPage"); // 设置纸张大小
	}
	LODOP.ADD_PRINT_TABLE(150, left, textWidth, tableHeight, getFollowTableStr(data)); // 表格打印
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2); // 1居左 2居中打印 3右对齐
	// LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_STYLEA(100, 'VOrient', 1);
	LODOP.ADD_PRINT_HTM(35, left, textWidth, 30, `<h1 style="text-align:center;margin:0 auto;font-size:26px">数航供应链统一配送单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 一维码
	LODOP.ADD_PRINT_BARCODE(20, left, 220, 75, '128Auto', data.followNumber);
	// LODOP.SET_PRINT_STYLEA(0, 'ShowBarText', 0);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 二维码
	LODOP.ADD_PRINT_BARCODE(20, "78%", "2.5cm", "2.5cm", 'QRCode', data.followNumber);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);

	LODOP.ADD_PRINT_HTM(98, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;"><p style="font-size:16px;margin:0;">随货单编号：${data.followNumber}</p><p style="font-size:16px;margin:0;">出货日期：${parseTime4(data.createTime)}</p></div>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(123, left, textWidth, 25, `<p style="font-size:16px;word-break:break-all;">备注：${data.remark}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 分页
	LODOP.ADD_PRINT_HTM(tableHeight + 150 + 55, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;">出货人：</p><p style="margin:0;">送货人：</p><p style="margin:0;">接货人：</p><p style="margin:0;"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true);
	if (isPDF) {
		LODOP.SET_PREVIEW_WINDOW(2, 1, 0, 760, 540, "随货单导出.开始导出");
		LODOP.PREVIEW();
	} else {
		// LODOP.PREVIEW();
		// LODOP.PRINT_DESIGN();
		LODOP.PRINT();
	}
	// LODOP.PRINT_DESIGN();
	// LODOP.SELECT_PRINTER();
}
// 随货单表格生成
export function getFollowTableStr(data) {
	let { storeFollowList } = data;
	// 假数据
	// storeFollowList = [...storeFollowList, ...storeFollowList, ...storeFollowList, ...storeFollowList, ...storeFollowList, ...storeFollowList,...storeFollowList,...storeFollowList,...storeFollowList,...storeFollowList]
	let style = `<style>
.print-tab {border-collapse: collapse;width:100% !important;text-align: center;margin: 0 auto;}
.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
.print-tab td{font-size:14px}
</style><body>`;
	let tableStr =
		"<table class='print-tab'>" +
		`<thead><tr><td>出货方</td><td colspan="2" style="text-align:left !important;">${data.warehouseName}</td><td>收货方</td><td colspan="5"  style="text-align:left !important;">${data.storeName}</td></tr>` +
		"<tr><td>商品条码</td><td style='text-align:left'>商品名称</td><td style='width:45px;'>数量</td><td>规格</td><td>件数</td><td>生产日期</td></tr></thead><tbody>" +
		storeFollowList
			.map(row => {
				return `<tr>
            <td>${row.barCode}</td>
            <td style="text-align:left">${row.commodityName}</td>
            <td>${row.procureCount}</td>
            <td>${row.deliveryNorms + '*' + row.standard}</td>
            <td>${row.procureNumber + '件'}</td>
            <td>${parseTime4(row.manufactureTime)}</td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}

// 打印收货单
export function printReceiving(data) {
	const printPageSet = store.getters.printSet; // 0: A4  1: 三联打印
	let width = ''  // 打印纸张宽度
	let textWidth = ''  // 打印内容区域大小
	let tableHeight = 310;  // 打印表格高度
	LODOP = getLodop();
	let left = 0  // 打印初始距离左侧位置
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	if (printPageSet === '0') {
		textWidth = '19cm'
		tableHeight = 870;
		left = 27;
		LODOP.PRINT_INIT("数航供应链_ERP系统_收货单打印_A4纸");
		LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4"); // 设置纸张大小
	} else {
		width = '24.1cm'
		textWidth = '21cm';
		tableHeight = 310;
		LODOP.PRINT_INITA(0, 0, width, "139.5mm", "数航供应链_ERP系统_收货单打印_三联纸");
		LODOP.SET_PRINT_PAGESIZE(1, width, "139.5mm", "CreateCustomPage"); // 设置纸张大小
	}
	LODOP.ADD_PRINT_TABLE(125, left, textWidth, tableHeight, getReceivingTableStr(data)); // 表格打印
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2); // 1居左 2居中打印 3右对齐
	// LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_STYLEA(100, 'VOrient', 1);
	LODOP.ADD_PRINT_HTM(20, left, textWidth, 30, `<h1 style="text-align:center;margin:0 auto;font-size:26px">数航供应链统一收货单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(60, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;width:40%">供应商:${data.supplierName}</p><p style="margin:0;width:30%">收货仓库:${data.warehouseName}</p><p style="margin:0;width:30%">来货单号:${data.goodsNumber}</p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);

	LODOP.ADD_PRINT_HTM(90, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;width:40%">收货单号:${data.number}</p><p style="margin:0;width:30%">起运时间:${parseTime4(data.departureTime)}</p><p style="margin:0;width:30%">到货时间:${parseTime4(data.arrivalTime)}</p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 分页
	LODOP.ADD_PRINT_HTM(tableHeight + 125 + 50, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;">收货人：</p><p style="margin:0;">送货人：</p><p style="margin:0;">采购人：</p><p style="margin:0;"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true);
	// LODOP.PREVIEW();
	// LODOP.PRINT_DESIGN();
	LODOP.PRINT();
}
// 收货单表格生成
export function getReceivingTableStr(data) {
	let { detailsList } = data;
	// 假数据
	// detailsList = [...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList]
	let style = `<style>
	.print-tab {border-collapse: collapse;width:100% !important;text-align: center;margin: 0 auto;}
	.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
	.print-tab td{font-size:14px}
	</style><body>`;
	let tableStr =
		"<table  class='print-tab'><thead><tr><th style='width:40px;'>序号</th><th style='text-align:left'>商品名称</th><th style='width:70px;'>采购数量</th><th style='width:70px;'>采购件数</th><th style='width:70px;'>收货数量</th><th style='width:70px;'>收货件数</th><th>规格</th><th style='width:40px;'>单位</th><th style='width:70px;'>生产日期</th></tr></thead><tbody>" +
		detailsList
			.map((row, index) => {
				return `<tr>
            <td>${index + 1}</td>
            <td style="text-align:left">${row.name}</td>
            <td>${row.buyNumber}</td>
            <td>${row.pieceNumber}</td>
            <td>${row.takeCount}</td>
            <td>${row.takePiece}</td>
            <td>${row.standard}</td>
            <td>${row.unitName}</td>
						<td>${parseTime4(row.manufactureTime)}</td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}

// 打印入库单
export function printInbound(data) {
	const printPageSet = store.getters.printSet; // 0: A4  1: 三联打印
	let width = ''  // 打印纸张宽度
	let textWidth = ''  // 打印内容区域大小
	let tableHeight = 310;  // 打印表格高度
	LODOP = getLodop();
	let left = 0  // 打印初始距离左侧位置
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	if (printPageSet === '0') {
		textWidth = '19cm'
		tableHeight = 870;
		left = 27;
		LODOP.PRINT_INIT("数航供应链_ERP系统_入库单打印_A4纸");
		LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4"); // 设置纸张大小
	} else {
		width = '24.1cm'
		textWidth = '21cm';
		tableHeight = 310;
		LODOP.PRINT_INITA(0, 0, width, "139.5mm", "数航供应链_ERP系统_入库单打印_三联纸");
		LODOP.SET_PRINT_PAGESIZE(1, width, "139.5mm", "CreateCustomPage"); // 设置纸张大小
	}
	LODOP.ADD_PRINT_TABLE(90, left, textWidth, tableHeight, getInboundTableStr(data)); // 表格打印
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2); // 1居左 2居中打印 3右对齐
	// LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_STYLEA(100, 'VOrient', 1);
	LODOP.ADD_PRINT_HTM(20, left, textWidth, 30, `<h1 style="text-align:center;margin:0 auto;font-size:26px">数航供应链统一入库单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(60, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;width:40%">供应商:${data.supplierName}</p><p style="margin:0;width:30%">入库仓库:${data.warehouseName}</p><p style="margin:0;width:30%">入库单号:${data.number}</p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);

	// LODOP.ADD_PRINT_HTM(90, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;width:40%">收货单号:${data.number}</p><p style="margin:0;width:30%">起运时间:${parseTime4(data.departureTime)}</p><p style="margin:0;width:30%">到货时间:${parseTime4(data.arrivalTime)}</p></div>`)
	// LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 分页
	LODOP.ADD_PRINT_HTM(tableHeight + 90 + 85, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;">入库人：</p><p style="margin:0;">入库时间：${parseTime4(data.inboundTime)}</p><p style="margin:0;"> </p><p style="margin:0;"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true);
	// LODOP.PREVIEW();
	// LODOP.PRINT_DESIGN();
	LODOP.PRINT();
}
// 入库单表格生成
export function getInboundTableStr(data) {
	let { detailsList } = data;
	// 假数据
	// detailsList = [...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList]
	let style = `<style>
	.print-tab {border-collapse: collapse;width:100% !important;text-align: center;margin: 0 auto;}
	.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
	.print-tab td{font-size:14px}
	</style><body>`;
	// 含税成本合计
	let allPrice = 0;
	// 件数合计
	let allPiece = 0;
	let tableStr =
		"<table  class='print-tab'><thead><tr><th style='width:40px;'>序号</th><th style='text-align:left'>商品名称</th><th style='width:70px;'>商品条码</th><th>规格</th><th style='width:70px;'>入库件数</th><th style='width:70px;'>入库数量</th><th style='width:80px;'>进价(元)</th><th style='width:100px;'>含税成本(元)</th></tr></thead><tbody>" +
		detailsList
			.map((row, index) => {
				let itemPrice = DecimalUtils.multiply(row.agreedPrice, row.inboundCount);
				allPiece = DecimalUtils.add(allPiece, row.inboundPiece);
				allPrice = DecimalUtils.add(allPrice, itemPrice);
				return `<tr>
            <td>${index + 1}</td>
            <td style="text-align:left">${row.name}</td>
            <td>${row.firstCode}</td>
            <td>${row.standard}</td>
            <td>${row.inboundPiece}</td>
            <td>${row.inboundCount}</td>
            <td>${row.agreedPrice}</td>
						<td>${itemPrice}</td>
          </tr>`;
			}).join('') + `<tr>
			<td>合计</td>
			<td> - </td>
			<td> - </td>
			<td> - </td>
			<td> ${allPiece} </td>
			<td> - </td>
			<td> - </td>
			<td>${allPrice}</td>
			</tr>` +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}


// 打印调拨单
export function printDbd(data) {
	const printPageSet = store.getters.printSet; // 0: A4  1: 三联打印
	let width = ''  // 打印纸张宽度
	let textWidth = ''  // 打印内容区域大小
	let tableHeight = 280;  // 打印表格高度
	LODOP = getLodop();
	let left = 0  // 打印初始距离左侧位置
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	if (printPageSet === '0') {
		textWidth = '19cm'
		tableHeight = 850;
		left = 27;
		LODOP.PRINT_INIT("数航供应链_ERP系统_入库单打印_A4纸");
		LODOP.SET_PRINT_PAGESIZE(1, 0, 0, "A4"); // 设置纸张大小
	} else {
		width = '24.1cm'
		textWidth = '21cm';
		tableHeight = 280;
		LODOP.PRINT_INITA(0, 0, width, "139.5mm", "数航供应链_ERP系统_调拨单打印_三联纸");
		LODOP.SET_PRINT_PAGESIZE(1, width, "139.5mm", "CreateCustomPage"); // 设置纸张大小
	}
	LODOP.ADD_PRINT_TABLE(155, left, textWidth, tableHeight, getDbdTableStr(data)); // 表格打印
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2); // 1居左 2居中打印 3右对齐
	// LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_STYLEA(100, 'VOrient', 1);
	LODOP.ADD_PRINT_HTM(35, left, textWidth, 30, `<h1 style="text-align:center;margin:0 auto;font-size:26px">数航供应链统一调拨单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 一维码
	LODOP.ADD_PRINT_BARCODE(20, left, 220, 75, '128Auto', data.allocationNumber);
	// LODOP.SET_PRINT_STYLEA(0, 'ShowBarText', 0);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 二维码
	LODOP.ADD_PRINT_BARCODE(20, "78%", "2.5cm", "2.5cm", 'QRCode', data.allocationNumber);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);

	LODOP.ADD_PRINT_HTM(105, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;"><p style="font-size:16px;margin:0;">调拨单编号：${data.allocationNumber}</p><p style="font-size:16px;margin:0;">调拨日期：${parseTime4(data.allocationTime)}</p></div>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(128, left, textWidth, 25, `<p style="font-size:16px;word-break:break-all;">备注：${data.remark || ''}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 分页
	LODOP.ADD_PRINT_HTM(tableHeight + 155 + 50, left, textWidth, 25, `<div style="padding:0;margin:0;display:flex;justify-content: space-between;font-weight:bold;font-size:16px;"><p style="margin:0;">出货人：</p><p style="margin:0;">送货人：</p><p style="margin:0;">接货人：</p><p style="margin:0;"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p></div>`)
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.SET_PRINT_MODE("POS_BASEON_PAPER", true);
	// LODOP.PREVIEW();
	// LODOP.PRINT_DESIGN();
	LODOP.PRINT();
}
// 调拨单表格生成
export function getDbdTableStr(data)  {
	let { detailsList } = data;
	// 假数据
	// detailsList = [...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList,...detailsList]
	let style = `<style>
.print-tab {border-collapse: collapse;width:100% !important;text-align: center;margin: 0 auto;}
.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
.print-tab td{font-size:14px}
</style><body>`;
	let tableStr =
		"<table class='print-tab'>" +
		`<thead><tr><td>出库门店</td><td style="text-align:left !important;">${data.allocationStoreName}</td><td style='min-width: 60px'>收货门店</td><td  style="text-align:left !important;">${data.receiveStoreName}</td><td>调拨人</td><td>${data.userName}</td></tr>` +
		"<tr><td style='width: 100px'>商品条码</td><td style='text-align:left'>商品名称</td><td style='width:45px;'>数量</td><td style='min-width: 100px'>规格</td><td style='min-width: 60px'>件数</td><td>生产日期</td></tr></thead><tbody>" +
		detailsList
			.map(row => {
				return `<tr>
            <td>${row.barCode}</td>
            <td style="text-align:left">${row.commodityName}</td>
            <td>${row.allocationCount}</td>
            <td>${row.commodityStandard}</td>
            <td>${row.allocationNumber + '件'}</td>
            <td>${parseTime4(row.manufactureTime)}</td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}



function WaitFor() {
	c = c + 1;
	console.log('正等待(JOB代码是' + P_ID + ')打印结果：' + c + '秒');
	t = setTimeout(WaitFor, 1000);
	if (LODOP.GET_VALUE('PRINT_STATUS_OK', P_ID)) {
		clearTimeout(t);
		console.log('走的lodop插件方式：PRINT_STATUS_OK判断为打印成功');
		c = 0;
		alert('打印成功！');
	}
	if (!LODOP.GET_VALUE('PRINT_STATUS_EXIST', P_ID) && c > 0) {
		clearTimeout(t);
		console.log('走的lodop插件方式：判断该该任务已经不在队列，已成功或删除任务');
		c = 0;
		alert('打印任务不在队列，成功或删除');
	} else if (c > 50) {
		clearTimeout(t);
		console.log('打印超过50秒没捕获到成功状态！');
		c = 0;
		alert('打印超过50秒没捕获到成功状态！');
	}
}

function C_WaitFor() {
	c = c + 1;
	console.log('正等待(JOB代码是' + P_ID + ')打印结果：' + c + '秒');
	t = setTimeout(C_WaitFor, 1000);
	LODOP.On_Return_Remain = true;
	LODOP.On_Return = function (TaskID, Value) {
		// console.log(Value, '@@@@@@@@@@@');
		// console.log(TaskID, 'TaskID');
		// console.log(TaskID1, 'TaskID1');
		// console.log(TaskID2, 'TaskID2');
		if (TaskID == TaskID1) {
			if (Value == 1) {
				alert(TaskID + ' ' + Value);
				clearTimeout(t);
				console.log('走的c-lodop方式：PRINT_STATUS_OK判断为打印成功');
				c = 0;
				alert('打印成功！');
			}
		} else if (TaskID == TaskID2) {
			if (Value == 0) {
				clearTimeout(t);
				console.log('走的c-lodop方式：判断该该任务已经不在队列，已成功或删除任务');
				c = 0;
				alert('打印任务不在队列，成功或删除');
			}
		}
	};
	TaskID1 = LODOP.GET_VALUE('PRINT_STATUS_OK', P_ID);
	TaskID2 = LODOP.GET_VALUE('PRINT_STATUS_EXIST', P_ID);
	if (c > 50) {
		clearTimeout(t);
		console.log('打印超过50秒没捕获到成功状态！');
		c = 0;
		alert('打印超过50秒没捕获到成功状态！');
	}
}

// 打印盘点单
export function printCheckInfo(data) {
	LODOP = getLodop();
	if (!LODOP) return;
	LODOP.SET_LICENSES('', 'EE0887D00FCC7D29375A695F728489A6', 'C94CEE276DB2187AE6B65D56B3FC2848', '');
	// LODOP.SET_LICENSES("","13F0BE65846276CB60111433C6280000","","");
	LODOP.PRINT_INITA('0mm', '0mm', '210mm', '297mm', '数航供应链_ERP系统_盘点单打印');
	LODOP.SET_PRINT_PAGESIZE(1, 0, 0, 'A4');
	LODOP.ADD_PRINT_TABLE(175, '0%', '100%', '80%', getCheckInfo(data));
	LODOP.SET_PRINT_STYLEA(0, 'ALIGNMENT', 2);
	LODOP.ADD_PRINT_HTM(25, '80.869%', 146, 28, `<p style="font-weight:bold;font-size:16px"><span tdata='pageNO'>第##页</span> <span tdata='pageCount'>共##页</span></p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(20, 0, '210mm', 30, `<h1 style="text-align:center;font-family:'楷体';margin:0 auto;font-size:26px">数航供应链统一盘点单</h1>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// 一维码
	// LODOP.ADD_PRINT_BARCODE(10, "6%", 220, 75,"128Auto",data.followNumber);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	LODOP.ADD_PRINT_HTM(70, 50, '100mm', 25, `<p style="font-size:16px">盘点人员：${data.userName}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(70, '35%', '30%', 25, `<p style="font-size:16px">盘点名称：${data.name}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(70, '65%', '100mm', 25, `<p style="font-size:16px">区域名称：${data.detailsList[0]?.region}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	LODOP.ADD_PRINT_HTM(100, 50, '183.09mm', 75, `<p style="font-size:16px;word-break:break-all;">备注：${data.remark}</p>`);
	LODOP.SET_PRINT_STYLEA(0, 'ItemType', 1);
	// LODOP.ADD_PRINT_HTM(500, 45, "100mm", 25, `<p style="font-size:16px">出货人：</p>`);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	// LODOP.ADD_PRINT_HTM(500, 245, "100mm", 25, `<p style="font-size:16px">送货人：</p>`);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	// LODOP.ADD_PRINT_HTM(500, 445, "100mm", 25, `<p style="font-size:16px">接货人：</p>`);
	// LODOP.SET_PRINT_STYLEA(0, "ItemType", 1);
	LODOP.PRINT();
	// LODOP.PRINT_DESIGN();
}
// 盘点单表格生成
export function getCheckInfo(data) {
	let { detailsList } = data;
	// detailsList = [...detailsList, ...detailsList];
	let style = `<style>
.print-tab {border-collapse: collapse;width: 700px !important;margin: 0 auto;}
.print-tab td,.print-tab th {text-align: center;border: 1px solid #000;padding: 3px;}
.print-tab td{font-size:14px}
</style><body>`;
	let tableStr =
		"<table class='print-tab'>" +
		"<thead><tr><td style='text-align:left'>商品名称</td><td style='width:70px;'>通道名称</td><td style='width:60px;'>仓位名称</td><td>盘点数量</td><td style='width:100px;'>备注</td></tr></thead><tbody>" +
		detailsList
			.map(row => {
				return `<tr>
            <td style="text-align:left">${row.name}</td>
            <td>${row.aisle}</td>
            <td>${row.position}</td>
            <td></td>
            <td></td>
          </tr>`;
			})
			.join('') +
		'</tbody></table>' +
		'</body>';
	return style + tableStr;
}
